package com.svmc.app.task.tabmanager;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

import com.svmc.app.task.activity.AddNewTask;
import com.svmc.app.task.activity.EditTask;
import com.svmc.app.task.utils.CommonDefine;

/**
 * The purpose of this Activity is to manage the activities in a tab. Note:
 * Child Activities can handle Key Presses before they are seen here.
 * 
 * @author Eric Harlow
 */
public class TabGroupActivity extends ActivityGroup implements CommonDefine {
	public static int stateActivity = 0;
	public static int id_data = 0;
	public static boolean isdestroy =false;
	public static boolean isstop=false;
	private ArrayList<String> mIdList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (mIdList == null)
			mIdList = new ArrayList<String>();
	}

	/**
	 * this is call a child activity anything with ID when a child activity
	 * killed by system in some scenario : rotate screen...
	 * 
	 * @param id
	 * @param newIntent
	 * @param flag
	 */
	public void replaceContentView(String id, Intent newIntent) {
		View view = getLocalActivityManager().startActivity(id,
				newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)).getDecorView();
		if (view != null) {
			mIdList.add(id);
			this.setContentView(view);
		}
	}

	/**
	 * This is called when a child activity of this one calls its finish method.
	 * This implementation calls {@link LocalActivityManager#destroyActivity} on
	 * the child activity and starts the previous activity. If the last child
	 * activity just called finish(),this activity (the parent), calls finish to
	 * finish the entire group.
	 */
	@Override
	public void finishFromChild(Activity child) {
		LocalActivityManager manager = getLocalActivityManager();
		int index = mIdList.size() - 1;

		if (index < 1) {
			finish();
			return;
		}

		manager.destroyActivity(mIdList.get(index), true);
		mIdList.remove(index);
		index--;
		String lastId = mIdList.get(index);
		Intent lastIntent = manager.getActivity(lastId).getIntent();
		Window newWindow = manager.startActivity(lastId, lastIntent);
		newWindow.getDecorView().setAnimation(outFromLeftAnimation());
		setContentView(newWindow.getDecorView());
	}

	/**
	 * Starts an Activity as a child Activity to this.
	 * 
	 * @param Id
	 *            Unique identifier of the activity to be started.
	 * @param intent
	 *            The Intent describing the activity to be started.
	 * @throws android.content.ActivityNotFoundException.
	 */
	public void startChildActivity(String Id, Intent intent) {
		Window window = getLocalActivityManager().startActivity(Id,
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		if (window != null) {
			mIdList.add(Id);
			window.getDecorView().setAnimation(inFromRightAnimation());
			setContentView(window.getDecorView());
		}
	}

	/**
	 * The primary purpose is to prevent systems before
	 * android.os.Build.VERSION_CODES.ECLAIR from calling their default
	 * KeyEvent.KEYCODE_BACK during onKeyDown.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// preventing default implementation previous to
			// android.os.Build.VERSION_CODES.ECLAIR
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * Overrides the default implementation for KeyEvent.KEYCODE_BACK so that
	 * all systems call onBackPressed().
	 */
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			onBackPressed();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

	/**
	 * If a Child Activity handles KeyEvent.KEYCODE_BACK. Simply override and
	 * add this method.
	 */
	@Override
	public void onBackPressed() {
		int length = mIdList.size();
		if (length >= 1) {
			Activity current = getLocalActivityManager().getActivity(
					mIdList.get(length - 1));
			current.finish();
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		try {
			int edit_or_new = data.getExtras().getInt(EDIT_OR_NEW);
			switch (requestCode) {
			case REQUEST_CODE_TIME_FINISH:
				if (edit_or_new == ACTIVITY_EDIT) {
					Log.i("edit_or_new", "" + edit_or_new);
					EditTask editactivity = (EditTask) getLocalActivityManager()
							.getCurrentActivity();
					editactivity.handeonActivityResult(requestCode, resultCode,
							data);
				} else if (edit_or_new == ACTIVITY_NEW) {
					Log.i("edit_or_new", "" + edit_or_new);
					AddNewTask newactivity = (AddNewTask) getLocalActivityManager()
							.getCurrentActivity();
					newactivity.handeonActivityResult(requestCode, resultCode,
							data);
				}
				break;
			case REQUEST_CODE_TIME_REMINDER:
				if (edit_or_new == ACTIVITY_EDIT) {
					EditTask editactivity = (EditTask) getLocalActivityManager()
							.getCurrentActivity();
					editactivity.handeonActivityResult(requestCode, resultCode,
							data);
				} else if (edit_or_new == ACTIVITY_NEW) {
					AddNewTask newactivity = (AddNewTask) getLocalActivityManager()
							.getCurrentActivity();
					newactivity.handeonActivityResult(requestCode, resultCode,
							data);
				}

				break;
			case REQUEST_CODE_SELECT_RINGTONE:
				if (edit_or_new == ACTIVITY_EDIT) {
					EditTask editactivity = (EditTask) getLocalActivityManager()
							.getCurrentActivity();
					editactivity.handeonActivityResult(requestCode, resultCode,
							data);
				} else if (edit_or_new == ACTIVITY_NEW) {
					AddNewTask newactivity = (AddNewTask) getLocalActivityManager()
							.getCurrentActivity();
					newactivity.handeonActivityResult(requestCode, resultCode,
							data);
				}
				break;
			default:
				break;

			}
		} catch (NullPointerException e) {
			// TODO: handle exception
		}

	}

	public Animation inFromRightAnimation() {
		Animation inFromRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(400);
		inFromRight.setInterpolator(new DecelerateInterpolator());
		return inFromRight;
	}

	public Animation outFromLeftAnimation() {
		Animation outFromleft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outFromleft.setDuration(400);
		outFromleft.setInterpolator(new AccelerateInterpolator());
		return outFromleft;
	}

	public void showToast(String text) {
		Toast.makeText(this, text, 0).show();
	}

}
